﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WJP.Events.Bus.Handlers;

namespace WJP.Events.Bus.Factories
{
    /// <summary>
    /// 这个<see cref="IEventHandlerFactory"/>实现被用来处理一个临时实例对象的事件。
    /// </summary>
    /// <typeparam name="THandler"></typeparam>
    internal class TransientEventHandlerFactory<THandler> : IEventHandlerFactory
        where THandler : IEventHandler, new()
    {
        /// <summary>
        /// 创建一个新的句柄实例对象
        /// </summary>
        /// <returns></returns>
        public IEventHandler GetHandler()
        {
            return new THandler();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public Type GetHandlerType()
        {
            return typeof(THandler);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="handler"></param>
        public void ReleaseHandler(IEventHandler handler)
        {
            if(handler is IDisposable)
            {
                (handler as IDisposable).Dispose();
            }
        }
    }
}
